﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace HQWeb.SysManage.FirstBroadcast
{
    public partial class FirstBroadcast : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindBranchDrop();
                //if (Convert.ToBoolean(Session["IsAll"]) == false)
                //{
                //    drop_branch.Enabled = false;
                //}
                string strBID = Session["BRANCH_ID"].ToString();
                drop_branch.SelectedValue = strBID;
                drop_branch.Enabled = false;
                drop_branch_SelectedIndexChanged(new object(), new EventArgs());
                BindList();
            }
        }

        #region 绑定列表数据
        private void BindList()
        {
            BLL.SysManage.T_PUB_BROADCAST bll = new BLL.SysManage.T_PUB_BROADCAST();
            //if (Convert.ToBoolean(Session["IsAll"]) == false)
            //{
                GridView1.DataSource = bll.GetBroadcastList(Convert.ToInt32(Session["BRANCH_ID"].ToString())); ;
            //}
            //else
            //{
            //    GridView1.DataSource = bll.GetBroadcastList(""); ;
            //}
            GridView1.DataBind();
        }
        #endregion

        #region 绑定分店列表
        private void BindBranchDrop()
        {
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            IList il = LibraryBll.GetModelList("");
            drop_branch.DataSource = il;
            drop_branch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            drop_branch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            drop_branch.DataBind();
            ListItem item = new ListItem("", "-1");
            drop_branch.Items.Add(item); 
        }
        #endregion

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 编辑

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int index = e.NewEditIndex;
            string strID = GridView1.DataKeys[index].Value.ToString();
            //string strID = GridView1.Rows[index].Cells[2].Text.ToString();
            BLL.SysManage.T_PUB_BROADCAST bll = new BLL.SysManage.T_PUB_BROADCAST();
            //Model.SysManage.T_PUB_BROADCAST model = new Model.SysManage.T_PUB_BROADCAST();
            IList<Model.SysManage.T_PUB_BROADCAST> broadcastList = new List<Model.SysManage.T_PUB_BROADCAST>();
            int temp;
            int.TryParse(strID, out temp);
            //model = bll.GetModel(tempID);
            drop_branch.SelectedValue = strID;
            //temp = (int)model.BRANCH_ID;
            broadcastList = bll.GetListByBranch(temp);
            BindData(temp, "1", textShow, show, show_id, broadcastList);
            BindData(temp, "2", textNewSchool, newSchool, newSchool_id, broadcastList);
            BindData(temp, "3", textMakeWedding, make, make_id, broadcastList);
            BindData(temp, "4", textHotel, hotel, hotel_id, broadcastList);
            BindData(temp, "5", textMyXilai, xilai, xilai_id, broadcastList);
            BindData(temp, "6", textThemeWedding, theme, theme_id, broadcastList);

        }


        #endregion

        #region 按钮事件

        //提交按钮
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BLL.SysManage.T_PUB_BROADCAST bll = new BLL.SysManage.T_PUB_BROADCAST();
            IList<Model.SysManage.T_PUB_BROADCAST> modelList = new List<Model.SysManage.T_PUB_BROADCAST>();
            IList<Model.SysManage.T_PUB_BROADCAST> addList = new List<Model.SysManage.T_PUB_BROADCAST>();
            string strID = drop_branch.SelectedValue;
            int temp;
            int.TryParse(strID, out temp);
            modelList = bll.GetListByBranch(temp);
            if (modelList.Count != 0)
            {
                //Model.SysManage.T_PUB_BROADCAST model = new Model.SysManage.T_PUB_BROADCAST();
                //
                IEnumerable<Model.SysManage.T_PUB_BROADCAST> model;
                //if (textShow.Value != "")
                //{
                    model = modelList.Where(p => p.MODULE == "1");
                    model.ElementAt(0).FILE_ID = show.Value;
                    model.ElementAt(0).FILE_TITEL = textShow.Value;
                    addList.Add(model.ElementAt(0));
                //}
                //
                //if (textNewSchool.Value != "")
                //{
                    model = modelList.Where(p => p.MODULE == "2");
                    model.ElementAt(0).FILE_ID = newSchool.Value;
                    model.ElementAt(0).FILE_TITEL = textNewSchool.Value;
                    addList.Add(model.ElementAt(0));
               // }
                //
                //if (textMakeWedding.Value != "")
                //{
                    model = modelList.Where(p => p.MODULE == "3");
                    model.ElementAt(0).FILE_ID = make.Value;
                    model.ElementAt(0).FILE_TITEL = textMakeWedding.Value;
                    addList.Add(model.ElementAt(0));
                //}
                //
                //if (textHotel.Value != "")
                //{
                    model = modelList.Where(p => p.MODULE == "4");
                    model.ElementAt(0).FILE_ID = hotel.Value;
                    model.ElementAt(0).FILE_TITEL = textHotel.Value;
                    addList.Add(model.ElementAt(0));
                //}
                //
                //if (textMyXilai.Value != "")
                //{
                    model = modelList.Where(p => p.MODULE == "5");
                    model.ElementAt(0).FILE_ID = xilai.Value;
                    model.ElementAt(0).FILE_TITEL = textMyXilai.Value;
                    addList.Add(model.ElementAt(0));
                //}
                //
                //if (textThemeWedding.Value != "")
                //{
                    model = modelList.Where(p => p.MODULE == "6");
                    model.ElementAt(0).FILE_ID = theme.Value;
                    model.ElementAt(0).FILE_TITEL = textThemeWedding.Value;
                    addList.Add(model.ElementAt(0));
                //}
                bll.UpdateData(addList);
                HQWeb.View.GetFistFile.AllFileList = null;
            }
            else
            {
                Model.SysManage.T_PUB_BROADCAST model1 = new Model.SysManage.T_PUB_BROADCAST();
                //
                model1.FILE_ID = show.Value;
                model1.FILE_TITEL = textShow.Value;
                model1.BRANCH_ID = temp;
                model1.MODULE = "1";
                addList.Add(model1);
                //
                Model.SysManage.T_PUB_BROADCAST model2 = new Model.SysManage.T_PUB_BROADCAST();
                model2.FILE_ID = newSchool.Value;
                model2.FILE_TITEL = textNewSchool.Value;
                model2.BRANCH_ID = temp;
                model2.MODULE = "2";
                addList.Add(model2);
                //
                Model.SysManage.T_PUB_BROADCAST model3 = new Model.SysManage.T_PUB_BROADCAST();
                model3.FILE_ID = make.Value;
                model3.FILE_TITEL = textMakeWedding.Value;
                model3.BRANCH_ID = temp;
                model3.MODULE = "3";
                addList.Add(model3);
                //
                Model.SysManage.T_PUB_BROADCAST model4 = new Model.SysManage.T_PUB_BROADCAST();
                model4.FILE_ID = hotel.Value;
                model4.FILE_TITEL = textHotel.Value;
                model4.BRANCH_ID = temp;
                model4.MODULE = "4";
                addList.Add(model4);
                //
                Model.SysManage.T_PUB_BROADCAST model5 = new Model.SysManage.T_PUB_BROADCAST();
                model5.FILE_ID = xilai.Value;
                model5.FILE_TITEL = textMyXilai.Value;
                model5.BRANCH_ID = temp;
                model5.MODULE = "5";
                addList.Add(model5);
                //
                Model.SysManage.T_PUB_BROADCAST model6 = new Model.SysManage.T_PUB_BROADCAST();
                model6.FILE_ID = theme.Value;
                model6.FILE_TITEL = textThemeWedding.Value;
                model6.BRANCH_ID = temp;
                model6.MODULE = "6";
                addList.Add(model6);
                bll.AddData(addList);
            }
            //btnCancle_Click(new object(), new EventArgs());
            BindList();
            Universal.JS.Alert("操作成功！");            
        }

        //取消
        protected void btnCancle_Click(object sender, EventArgs e)
        {
            drop_branch.SelectedValue = "-1";
            textShow.Value = "";
            show.Value = "";
            textNewSchool.Value = "";
            newSchool.Value = "";
            textMakeWedding.Value = "";
            make.Value = "";
            textHotel.Value = "";
            hotel.Value = "";
            textMyXilai.Value = "";
            xilai.Value = "";
            textThemeWedding.Value = "";
            theme.Value = "";
            btnSubmit.Text = "编 辑";
            labTitle.Text = "初始化播放视频";

        }

        #endregion

        #region 绑定数据
        private void BindData(int branch_id, string item, System.Web.UI.HtmlControls.HtmlInputText asptxt, System.Web.UI.HtmlControls.HtmlInputText webtxt, System.Web.UI.HtmlControls.HtmlInputText id, IList<Model.SysManage.T_PUB_BROADCAST> broadcastList)
        {
            //BLL.SysManage.T_PUB_BROADCAST bll = new BLL.SysManage.T_PUB_BROADCAST();
            //IList<Model.SysManage.T_PUB_BROADCAST> broadcastList = new List<Model.SysManage.T_PUB_BROADCAST>();
            //broadcastList = bll.GetListByBranch(branch_id);
            Model.SysManage.T_PUB_BROADCAST model = new Model.SysManage.T_PUB_BROADCAST();
            model = broadcastList.Where(p => p.MODULE == item).ElementAt(0);
            asptxt.Value = model.FILE_TITEL;
            webtxt.Value = model.FILE_ID;
            id.Value = model.ID.ToString();
        }
        #endregion

        protected void drop_branch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drop_branch.SelectedValue != "-1")
            {
                int branch_id = Convert.ToInt32(drop_branch.SelectedValue);
                BLL.SysManage.T_PUB_BROADCAST bll = new BLL.SysManage.T_PUB_BROADCAST();
                IList<Model.SysManage.T_PUB_BROADCAST> broadcastList = new List<Model.SysManage.T_PUB_BROADCAST>();
                broadcastList = bll.GetListByBranch(branch_id);
                if (broadcastList.Count != 0)
                {
                    BindData(branch_id, "1", textShow, show, show_id, broadcastList);
                    BindData(branch_id, "2", textNewSchool, newSchool, newSchool_id, broadcastList);
                    BindData(branch_id, "3", textMakeWedding, make, make_id, broadcastList);
                    BindData(branch_id, "4", textHotel, hotel, hotel_id, broadcastList);
                    BindData(branch_id, "5", textMyXilai, xilai, xilai_id, broadcastList);
                    BindData(branch_id, "6", textThemeWedding, theme, theme_id, broadcastList);
                }
                else
                {
                    btnCancle_Click(new object(), new EventArgs());
                    drop_branch.SelectedValue = branch_id.ToString();
                }
            }
        }

        protected string getModule(string module)
        {
            string str = "";
            switch (module)
            {
                case "1": str = "喜来展示"; break;
                case "2": str = "新人学堂"; break;
                case "3": str = "婚礼定制"; break;
                case "4": str = "酒店展示"; break;
                case "5": str = "我的喜来"; break;
                case "6": str = "主题庆典"; break;
                default:
                    break;
            }
            return str;
        }
    }
}